//
//  AppDelegate.swift
//  weibo
//
//  Created by 张宏芝 on 15/8/29.
//  Copyright © 2015年 kama. All rights reserved.
//

import UIKit
import AFNetworking

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//        打印用户的账号信息
        printLog(UserAccount.loadUserAccount())
        
        
/// 注册通知
/// object:监听由哪个对象发出的通知,设置为nil就是监听所有对象发出的'name'的通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchRootViewController:", name: HMSwitchRootViewControllerNotification, object: nil)
//
        setupNetWork()
         setupColor()
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        
//       window?.rootViewController = MainViewController()
        window?.rootViewController = defaultRootViewController()
//        window?.rootViewController = WelcomeViewController()
//        window?.rootViewController = NewFeatureCollectionView()
        
        window?.makeKeyAndVisible()
        
//        可以改变UINavigationBar与UITabBar的字体的颜色,全部替换为红色
//        UIApplication.sharedApplication().keyWindow?.tintColor = UIColor.redColor()
        printLog(isNewVersion())
        return true
    }
    
/// 注销通知:也可以不注销(个人习惯)
/// 在程序被销毁的时候才会调用这个方法,可以不写
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self, name: HMSwitchRootViewControllerNotification, object: nil)
    }
// 切换控制器的通知监听方法
    // 只是传递消息:post name 
    // 传递消息的同时,传递一个参数.通过"object"传递数值
    // 传递消息的同时,传递更多的内容,通过userInfo字典来传递数值
    func switchRootViewController(notification:NSNotification){
        printLog(notification)
        window?.rootViewController = (notification.object == nil) ? MainViewController() : WelcomeViewController()
    }
    
//    设置默认的根控制器
    private func defaultRootViewController()->UIViewController{
        if UserAccountViewModel.sharedUserAccount.userLogon{
            return isNewVersion() ? NewFeatureCollectionView():WelcomeViewController()
        }
        return MainViewController()
    }
    
//    检查不是不是新的版本
    private func isNewVersion()->Bool{
//        当前版本号
        let bundleVersion = Double(NSBundle .mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String)!
        printLog(bundleVersion)
//        之前保存的版本号
        let versionKey = "com.kama.weibo.version"
        let saxboxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(versionKey)
        printLog(saxboxVersion)
//        将当前的版本保存
        NSUserDefaults.standardUserDefaults().setDouble(bundleVersion, forKey: versionKey)
        
//        比较两个版本的大小,返回结果
        return bundleVersion > saxboxVersion
    }
    
//  设置导航条与tabBar按钮的颜色
    private func setupColor() {
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        UITabBar.appearance().tintColor = UIColor.orangeColor()
    }
    
//    设置网络指示器
    private func setupNetWork(){
        AFNetworkActivityIndicatorManager.sharedManager().enabled = true
//        NSURLCache:GET请求的缓存会被保存
        let cache = NSURLCache(memoryCapacity: 6*1024*1024, diskCapacity: 30*1024*1024, diskPath: nil)
        NSURLCache.setSharedURLCache(cache)
    }
    
}

